package com.example.im

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.text.TextUtils
import cc.vv.lkimcomponent.lkim.LKIMManager
import cc.vv.lkimcomponent.lkim.inter.LKIMOperateInter
import cc.vv.lkimcomponent.lkim.operate.LKIMOperate
import cc.vv.lklibrary.log.LogOperate
import com.example.BaseNewApplication
import com.example.im.monitor.ConnectionMonitor
import com.example.im.monitor.GroupMonitor
import com.example.im.monitor.MessageMonitor

/**
 * @author  chaohaipeng  on 2018/7/23.
 */
class IMInit{
    private object mHolder {
        val instance = IMInit()
    }
    companion object {
        fun getInstance(): IMInit {
            return mHolder.instance
        }
    }
    /**
     * 初始化监听、SDK
     */
    fun init(application: Application?) {
        val processPackageName = getProcessPackageName()
        val packageName = BaseNewApplication.getInstance()?.packageName
        LKIMManager.getInstance().addConnectionListener(ConnectionMonitor.getInstance())
        if (TextUtils.isEmpty(processPackageName) || TextUtils.isEmpty(packageName) || !processPackageName.equals(packageName, ignoreCase = true)) {
            return
        }
        if(application!=null){
            LKIMManager.getInstance().addGroupListener(GroupMonitor.getInstance())
            LKIMManager.getInstance().addMsgListener(MessageMonitor.getInstance())
            LKIMOperate.getInstance().initSDK(application, "1193180731228737#sk")
        }

    }

    /**
     * 登录
     */
    fun login(account:String) {
        if (TextUtils.isEmpty(account)){
            return
        }
        LKIMOperate.getInstance().login(account, "sk_hx_12346.", object : LKIMOperateInter() {

            override fun onSuccess() {
                LogOperate.e("登录IM成功")
                IMReceiveService.getInstance().sendRefreshConversation()
            }

            override fun onProgress(progress: Int, status: String?) {}

            override fun onError(code: Int, message: String?) {
                LogOperate.e("登录IM失败"+message)
            }
        })
    }

    /**
     * 退出
     *
     * @param operateInter 操作回调
     */
    fun exit(operateInter: LKIMOperateInter) {
        LKIMOperate.getInstance().exit(operateInter)
    }

    /**
     * 注册帐号
     *
     * @param account  帐号
     * @param password 密码
     */
    fun regist(account: String, password: String) {
        Thread(Runnable { LKIMOperate.getInstance().regist(account, password) }).start()
    }

    /**
     * 获取程序包名
     *
     * @return
     */
    private fun getProcessPackageName(): String {
        var processPackageName = ""
        val activityManager = BaseNewApplication.getInstance()?.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        if (null != activityManager) {
            val runningAppProcesses = activityManager.runningAppProcesses
            val iterator = runningAppProcesses.iterator()
            while (iterator.hasNext()) {
                val info = iterator.next() as ActivityManager.RunningAppProcessInfo
                try {
                    if (info.pid == android.os.Process.myPid()) {
                        processPackageName = info.processName
                        return processPackageName
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }

            }
        }
        return processPackageName
    }



}